package com.hertz.iris.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.hertz.iris.pojo.NormalAlarm;

public class DateUtil {

	public static final long DAY_MIL = 60*60*24*1000;
	
	public static final String Y_M_D = "yyyy-MM-dd";
	public static final String YMD_HM = "yyyy-MM-dd HH:mm";
	
	public static String timeFormat(long millsec,String format){
		Calendar 		calendar = Calendar.getInstance();
		if(millsec > 0 )
			calendar.setTimeInMillis(millsec);
		SimpleDateFormat  sdf = new SimpleDateFormat(format);
		return sdf.format(calendar.getTime());
	}
	
	public static String today(String format){
		long curretn = System.currentTimeMillis();
		return timeFormat(curretn, format);
	}
	
	public static long getNextAlarm(NormalAlarm n){
		if(n == null)
			return -1;
		Calendar c = Calendar.getInstance();
		
		Calendar c2 = Calendar.getInstance();
		c2.set(Calendar.HOUR_OF_DAY, n.getHh());
    	c2.set(Calendar.MINUTE, n.getMm());
    	c2.set(Calendar.SECOND, 0);
    	c2.set(Calendar.MILLISECOND, 0);
    	String[] weekValues = n.getWeeks().split(",");
    	boolean hasLoopValue = hasLoopValue(n.getWeeks());
    	
    	int from = c2.getTimeInMillis() <= c.getTimeInMillis() ? 1 : 0;
    	c2.add(Calendar.DAY_OF_YEAR, from);
    	if(hasLoopValue){
    		for(int i = 0 + from; i < 6 + from; i++){
    			
    			int dayOfWeek = c2.get(Calendar.DAY_OF_WEEK);
    			if(weekValues[(dayOfWeek + 5) % 7].equals("1")){
    				break;
    			}else{
    				c2.add(Calendar.DAY_OF_YEAR, 1);
    			}
    		}
    		return c2.getTimeInMillis();
    	}else{
    		if(c2.getTimeInMillis() <= c.getTimeInMillis() && !hasLoopValue){
        		c2.add(Calendar.DAY_OF_YEAR, 1);
        		return c2.getTimeInMillis();
        	}else{
        		return c2.getTimeInMillis();
        	}
    	}
    	
	}
	
	public static boolean hasLoopValue(String weekValues){
		if(Strings.isBlank(weekValues))
			return false;
		String[] arr = weekValues.split(",");
		boolean result = false;
		for(String value : arr){
			if(value.equals("1")){
				result = true;
				return result;
			}
		}
		return result;
	}
	
	public static boolean hasLoop(byte[] weekValues){
		if(weekValues == null || weekValues.length == 0)
			return false;
		else{
			boolean result = false;
			for(byte value : weekValues){
				if(value == 1){
					result = true;
					return result;
				}
			}
			return result;
		}
	}
	
	public static String getNextDay(int days){
		Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_YEAR, days);
        return DateUtil.timeFormat(c.getTimeInMillis(), DateUtil.Y_M_D);
	}
	
	public static long string2Time(String time,String format){
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		long result = 0;
		try {
			Date date = formatter.parse(time);
			result = date.getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return result;
	}
}
